package org.elzat.healthboxgatewayapp.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elzat.healthboxgatewayapp.util.ResultEntity;
import org.elzat.healthboxgatewayapp.controller.UserRegisterController.HealthBoxUserRegisterVO;
import org.elzat.healthboxgatewayapp.service.HealthBoxUserModifyService;
import org.elzat.healthboxgatewayapp.service.MessageTokenService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import static org.elzat.healthboxgatewayapp.controller.UserRegisterController.register;
import static org.elzat.healthboxgatewayapp.credentials.HealthBoxUserRole.*;

@RestController
@RequestMapping(value = "health-box-api/v1/users",
        produces = "application/json; charset=utf-8")
@AllArgsConstructor
@Slf4j
public class AdminRegisterController {

    private final HealthBoxUserModifyService modifyService;
    private final MessageTokenService tokenService;


    /**
     * 管理员注册接口, 只为系统管理员开放
     * 包含两种接口:
     *      1. POST .../users/system     注册系统管理员
     *      2. POST .../users/organ      注册机构管理员
     * 接口包含以下五个参数:
     *      1. userPhone    用户手机号
     *      2. idNumber     用户身份证号
     *      3. name         用户名
     *      4. password     用户密码
     *      5. token        短信验证码
     * 五个参数都是必要参数, 缺一不可
     *
     * 返回结果为data为ResultEntity类对象:
     *      {
     *          code: [成功返回200, 失败返回400]
     *          msg: [返回对应反馈消息]
     *          data: 失败时为null, 成功时
     *              {
     *                  "userPhone": [用户手机号],
     *                  "name": [用户名],
     *                  "idNumber": [用户身份证号],
     *                  "role": [用户角色]
     *              }
     *      }
     * */
    @PostMapping("system")
    public ResultEntity<HealthBoxUserRegisterVO> registerSystemAdmin(
            @RequestParam String userPhone,
            @RequestParam String idNumber,
            @RequestParam String name,
            @RequestParam String password,
            @RequestParam String token
    ) {
        log.info("got request to register system admin");
        ResultEntity<HealthBoxUserRegisterVO> result = register(userPhone, idNumber, name,
                password, token, SYSTEM_ADMIN, modifyService, tokenService);
        log.info("got registration result: " + result);
        return result;
    }

    @PostMapping("organ")
    public ResultEntity<HealthBoxUserRegisterVO> registerOrganAdmin(
            @RequestParam String userPhone,
            @RequestParam String idNumber,
            @RequestParam String name,
            @RequestParam String password,
            @RequestParam String token
    ) {
        log.info("got request to register organ admin");
        ResultEntity<HealthBoxUserRegisterVO> result = register(userPhone, idNumber, name,
                password, token, ORGAN_ADMIN, modifyService, tokenService);
        log.info("got registration result: " + result);
        return result;
    }
}
